//
//  SandBoxStore.m
//  5.25知识点UI17_本地存储(Sandbox)
//
//  Created by dingcong on 16/5/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SandBoxStore.h"

@implementation SandBoxStore

#pragma mark - Public

+ (instancetype)sharedSandBoxStore
{
    
    static SandBoxStore *sbs = nil;
    
    if (sbs == nil)
    {
        sbs = [[SandBoxStore alloc]init];
    }
    
    return sbs;
    
}
- (NSString *)sandboxPath
{
    //核心API
    return  NSHomeDirectory();
      
}
- (NSString *)pathForDocunmetDir
{
    //核心API
   return  [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject];
}
- (NSString *)pathForLibrary
{
    
    
    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, NO)lastObject];
  
}
- (NSString *)pathForCache
{
    
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    
    
}
- (NSString *)pathForApp
{
    
    return [[NSBundle mainBundle]bundlePath];
    
}
- (BOOL)storeFileWithString:(NSString *)content
                     forKey:(NSString *)key
{
    //核心API
    
      NSString *PathOfFile = [[self pathForDocunmetDir]stringByAppendingPathComponent:key];
   return [content writeToFile:PathOfFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
}
- (NSString *)readStringForKey:(NSString *)key
{
    
    NSString *pathOfFile = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    [NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];
    
    return pathOfFile;
    
}
- (BOOL)storeFileWithArray:(NSArray *)content
                       forkey:(NSString *)key
{
    NSString *path = [[ self pathForDocunmetDir]stringByAppendingString:key];
    
   return  [content writeToFile:path atomically:YES];
    
}

- (NSArray *)readArrayforkey:(NSString *)key
{
    
    NSString *path = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    return [NSArray arrayWithContentsOfFile:path];
    
}

- (BOOL)storeFileWithDictionary:(NSDictionary *)content
                         forkey:(NSString *)key
{
    
    NSString *path = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    return [content writeToFile:path atomically:YES];
    
    
}
- (NSDictionary *)reatDictionryForKey:(NSString *)key
{
    NSString *path = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    return [NSDictionary dictionaryWithContentsOfFile:path];
}
- (BOOL)storeFileWithimage:(UIImage *)image
                    forKey:(NSString *)key{
    
    //核心API  调用函数(jpg) 原图为1 不压缩, 像素最好
    NSData *dataOfJpg = UIImageJPEGRepresentation(image, 1);
    //如果这个值为空, 则调换png函数;
    if (!dataOfJpg)
    {
        dataOfJpg = UIImagePNGRepresentation(image);
        
    }
   return  [self storeFileWithData:dataOfJpg forkey:key];
    
    
    
}
- (BOOL)storeFileWithData:(NSData *)content
                   forkey:(NSString *)key
{
    NSString *path = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    return  [content writeToFile:path atomically:YES];
    
    
}



- (BOOL)storeFileWithimageData:(NSData *)imageData
                        forKey:(NSString *)key
{
   return  [self storeFileWithData:imageData forkey:key];
}

- (BOOL)storeByArchiverWithObject:(id)object forkey:(NSString *)key
{
    NSMutableData *mData = [NSMutableData data];
    //*归档过程
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    
    [archiver encodeObject:object forKey:key];
    
    [archiver finishEncoding];
    
    
    //data存储
    return [self storeFileWithData:mData forkey:key];
    
    
    
}


- (id)readByUnArchiverWithKey:(NSString *)key{
    
    //读取data(数据)
    
    NSString *path = [[self pathForDocunmetDir]stringByAppendingString:key];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    //反归档
    
    NSKeyedUnarchiver *unAchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    
   id result = [unAchiver decodeObjectForKey:key];
    
    [unAchiver finishDecoding];
    return result;
    
}

// 计算单个文件的大小
- (long long)fileSizeAtPath:(NSString *)filePath{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath])
    {
        return [[manager attributesOfItemAtPath:filePath error:nil]fileSize];
    }
    return 0;
    
}

//遍历文件夹获得文件夹大小.(返回 兆M)
- (float)folderSizeAtPath:(NSString *)folderPatth{
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPatth])
    {
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPatth]objectEnumerator];
    
    NSString *fileName;
    
    long long folderSize = 0;
    
    if ((fileName = [childFilesEnumerator nextObject]) != nil)
    {
        NSString *fileAbsolutePath = [folderPatth stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    NSLog(@"%lld",folderSize);
    
    return folderSize / 1024.0;
}

- (void)clearCachFile
{
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    
    NSArray *files = [[NSFileManager defaultManager]subpathsAtPath:cachPath];
    
    NSLog(@"cach路径!!%@", cachPath);
    
    for (NSString *dc in files)
    {
        NSError * error = nil ;
       NSString *path = [cachPath stringByAppendingPathComponent:dc];
        if ([[NSFileManager defaultManager]fileExistsAtPath:cachPath])
        {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            
        }
        
    }
    
    [self performSelectorOnMainThread:@selector(clearCachSuccess) withObject:nil waitUntilDone:YES];

    
}

- (void)clearCachSuccess{
    
    NSLog(@"清理成功");
    
}



#pragma mark - Private

@end
